package com.time.workshop.ui;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.time.workshop.API;
import com.time.workshop.App;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.adapter.AbsViewHolderAdapter;
import com.time.workshop.bean.BaseListResponse;
import com.time.workshop.bean.TaskList;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;
import com.time.workshop.view.RefreshViewLoadMoreProxy;
import com.time.workshop.view.RefreshViewLoadMoreProxy.OnLoadDataActionListener;

/**
 * 我的任务列表
 * 
 * @author fanbaolong
 *
 */
public class MyTaskListActivity extends SuperActivity implements OnLoadDataActionListener
{

	@V
	Button task_titlebar_left_btn;
	@V
	RadioButton task_radio0, task_radio1;
	@V
	RelativeLayout task_empty_relat;

	PullToRefreshListView mRefreshListView;
	RefreshViewLoadMoreProxy mLoadMoreProxy;
	private TaskAdapter mAdapter;

	private int mState = 0;

	private int nowPage = 1;

	private class Response extends BaseListResponse<TaskList>
	{
	}

	private Response mResponse;

	private List<TaskList> mTaskList = new ArrayList<TaskList>();

	@Override
	protected void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.activity_my_task_list);
		init();

	}

	private void init()
	{
		Injector.getInstance().inject(this);

		task_titlebar_left_btn.setOnClickListener(this);
		task_radio0.setOnClickListener(this);
		task_radio1.setOnClickListener(this);

		mRefreshListView = (PullToRefreshListView) findViewById(R.id.task_list);
		mRefreshListView.setEmptyView(task_empty_relat);
		mRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));

		mAdapter = new TaskAdapter(this, R.layout.item_task_list);
		mRefreshListView.setAdapter(mAdapter);

		mLoadMoreProxy = new RefreshViewLoadMoreProxy(mRefreshListView, Mode.BOTH, 0, 0);
		mLoadMoreProxy.setOnLoadDataActionListener(this);

		mRefreshListView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				Intent intent = new Intent(MyTaskListActivity.this, TimeHallDetailActivity.class);
				intent.putExtra("ID", mAdapter.getData().get(position-1).getID());
				startActivity(intent, true);
			}

		});

		getData(mState, 1, true);

	}

	/**
	 * 请求数据
	 * 
	 * @param state
	 * @param page
	 * @param isRefresh
	 */
	private void getData(int state, int page, final boolean isRefresh)
	{
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		String Uri = "";

		if(state == 0){
			params.put("token", PreferenceUtil.getString(getApplicationContext(), BaseConstant.PREF_TOKE_STRING));
			params.put("page", nowPage + "");
			Uri = API.task_user_accept;

		}else if(state == 1){

			params.put("token", PreferenceUtil.getString(getApplicationContext(), BaseConstant.PREF_TOKE_STRING));
			params.put("page", nowPage + "");
			Uri = API.task_user_need;
		}

		fh.get(Uri, params, new AjaxCallBack<Object>()
				{
			@Override
			public void onSuccess(Object t)
			{
				super.onSuccess(t);

				Gson gson = new Gson();
				mResponse = gson.fromJson(t.toString(), Response.class);

				if (mResponse.isSuccess())
				{

					mTaskList = mResponse.getResult();
					App.showLog("mTaskLis=="+mTaskList);

					if (isRefresh)
						mAdapter.update(mTaskList);
					else{

						if(mTaskList.size()== 0){
							NewToast.makeText(getApplicationContext(), getString(R.string.no_date));
						}
						mAdapter.append(mTaskList);

					}
					mRefreshListView.onRefreshComplete();

				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg)
			{
				super.onFailure(t, errorNo, strMsg);
				mRefreshListView.onRefreshComplete();
			}

				});

	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.task_titlebar_left_btn:
			back();
			break;
		case R.id.task_radio0:
			mState = 0;
			nowPage = 1;
			getData(mState, nowPage, true);

			mRefreshListView.setSelection(0);

			break;
		case R.id.task_radio1:
			mState = 1;
			nowPage = 1;

			getData(mState, nowPage, true);
			mRefreshListView.setSelection(0);
			break;
		default:
			break;
		}

	}

	@Override
	public void onRefresh(int page)
	{
		nowPage = 1;
		getData(mState, 1, true);
	}

	@Override
	public void onLoadMore(int page)
	{
		nowPage++;
		getData(mState, nowPage, false);

	}

	private class TaskAdapter extends AbsViewHolderAdapter<TaskList>
	{

		public TaskAdapter(Context context, int layoutRes)
		{
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, TaskList itemData)
		{
			TextView mStateText = getViewFromHolder(R.id.item_task_state_text);
			TextView mTitleText = getViewFromHolder(R.id.item_task_title);
			TextView mTimeText = getViewFromHolder(R.id.item_task_time);
			TextView mPriceText = getViewFromHolder(R.id.item_task_price);

			mStateText.setText(itemData.getSTATUS_NAME());
			mTitleText.setText(itemData.getTITLE());
			mTimeText.setText(itemData.getU_TIME());
			mPriceText.setText(itemData.getPRICE() == null? "":(itemData.getPRICE() + "元"));

		}

	}

}
